package com.hutao.manage.service.shiro;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hutao.manage.pojo.User;
import com.hutao.manage.util.HttpClientUtil;
import com.hutao.manage.util.Result;

public class ShiroRealmNewImpl extends AuthorizingRealm {

	@Value("${login.url.new1}")
	private String loginUrlNew1;

	@Value("${findAuthorityList.url.new}")
	private String findAuthorityListUrlNew;

	/**
	 * 为当前登录的用户授予角色和权限
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Map<String, String> params = new HashMap<>();
		Set<String> authoritySet = new HashSet<>();
		
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User) session.getAttribute("user");
		
		if(user!=null){
			params.put("userId", user.getId());
			params.put("medicalMainstayId", user.getMedicalMainstayId());

			String authorityStr = HttpClientUtil.doPost(findAuthorityListUrlNew, params);
			Result result = JSONObject.parseObject(authorityStr, Result.class);
			if (result.isSuccess()) {
				JSONArray authJsonArray = (JSONArray) result.getData();
				for (int i = 0; i < authJsonArray.size(); i++) {
					authoritySet.add((String) authJsonArray.get(i));

				}

			}
		}

		// 设置权限
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		authorizationInfo.setStringPermissions(authoritySet);
		return authorizationInfo;

	}

	/**
	 * 用户登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String loginName = (String) token.getPrincipal();
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();

		Map<String, String> params = new HashMap<>();
		params.put("username", loginName);
		params.put("contextPath", (String) session.getAttribute("contextPath"));
		// 远程调用sso登录系统
		String userJson = HttpClientUtil.doPost(loginUrlNew1, params);

		JSONObject jsonObject = JSONObject.parseObject(userJson);
		if ((boolean) jsonObject.get("isSuccess")) {
			String userName = (String) jsonObject.get("usreName");
			String password = (String) jsonObject.get("password");
			AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(userName, password, "xx");
			return authcInfo;

		} else {
			return null;
		}

	}

}
